/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.PhanCong_DTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class PhanCong_DAO {
    
    public static ArrayList<PhanCong_DTO> GetALL()
    {
        ArrayList<PhanCong_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from PhanCong" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                PhanCong_DTO PhanCong = new PhanCong_DTO();
                PhanCong.setID(rs.getInt("ID"));
                PhanCong.setMaNV(rs.getString("MaNV"));
                PhanCong.setMaNVPC(rs.getString("MaNVPC"));
                PhanCong.setMaDT(rs.getString("MaDT"));
                PhanCong.setVaiTro(rs.getString("VaiTro"));
                PhanCong.setNgayPC(rs.getDate("NgayPC"));
                PhanCong.setGhiChu(rs.getString("GhiChu"));
                ds.add(PhanCong);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(PhanCong_DTO PhanCong)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgayPC = fmt.format(PhanCong.getNgayPC());
            String sql = "Insert PhanCong(MaNV,MaNVPC,MaDT,VaiTro,NgayPC,GhiChu) Values('"+PhanCong.getMaNV()+"','"+PhanCong.getMaNVPC()+"','"+PhanCong.getMaDT()+"','"+PhanCong.getVaiTro()+"','"+NgayPC+"','"+PhanCong.getGhiChu()+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(PhanCong_DTO PhanCong)
    {
        boolean flag = false;
        try {
            java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
            String NgayPC = fmt.format(PhanCong.getNgayPC());
            String sql = "Update PhanCong Set MaNV = '"+ PhanCong.getMaNV() +"', MaNVPC = '"+PhanCong.getMaNVPC()+"', MaDT = '"+PhanCong.getMaDT()+"', VaiTro ='"+PhanCong.getVaiTro()+"', NgayPC = '"+ NgayPC+ "', GhiChu='"+PhanCong.getGhiChu()+"' where ID = "+ PhanCong.getID();
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(int maPC)
    {
        boolean flag = false;
        try {
            String sql = "Delete from PhanCong where ID = "+ maPC;
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
}
